import time

import pygame
from tools.tools import *

GAME_TITLE = "战争雷霆"
SCREEN_RECT = pygame.Rect(0, 0, 480, 900)
FRAME_PER_SEC = 100
CREATE_ENEMY_EVENT = pygame.USEREVENT
PLAYER_SPEED = 5
BORN_DELAY = 3
BIG_TIMES = 1.5
COLOR_KEY = (255, 0, 255)
BULLET_SPEED = -33
HERO_FIRE_EVENT = pygame.USEREVENT + 1


class GameSprite(pygame.sprite.Sprite):
    """飞机大战游戏精灵"""

    def __init__(self, image_name, speed=1):
        super().__init__()
        self.image = pygame.image.load(image_name)
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self):
        self.rect.y = self.rect.y + self.speed


class Background(GameSprite):
    """背景类"""

    # 加载背景图片，当前只加载一张，后面考虑根据入参加载
    def __init__(self):
        super().__init__("../resources/images//Fly/BackScr/Fly_BackScr1.png")

    def update(self):
        super().update()
        # 背景交替滚动，到达底部之后，回到最上方
        if self.rect.y >= SCREEN_RECT.height:
            self.rect.y = -2 * self.rect.height + SCREEN_RECT.height


class Player(pygame.sprite.Sprite):
    """玩家类"""

    # 创建玩家战机，设置x，y方向的速度
    def __init__(self, player_code, x_speed=0, y_speed=0):
        super(Player, self).__init__()
        self.isUnbeatable = True
        self.x_speed = x_speed
        self.y_speed = y_speed

        # 加载所有玩家战机
        all_plane_graphics = load_graphics("../resources/images/Fly/Player")

        # 不同姿态的飞机，静止，左移，右移
        self.static_image = get_image(all_plane_graphics[player_code], 0, 0, 26, 32, COLOR_KEY, BIG_TIMES)
        self.up_image = get_image(all_plane_graphics[player_code], 26, 0, 26, 32, COLOR_KEY, BIG_TIMES)
        self.left_image = get_image(all_plane_graphics[player_code], 26 * 2, 0, 26, 32, COLOR_KEY, BIG_TIMES)
        self.right_image = get_image(all_plane_graphics[player_code], 26 * 3, 0, 26, 32, COLOR_KEY, BIG_TIMES)
        self.fire_image = get_image(all_plane_graphics[player_code], 26 * 4, 0, 26, 32, COLOR_KEY, BIG_TIMES)
        self.born_image = get_image(all_plane_graphics[player_code], 26 * 5, 0, 26, 32, COLOR_KEY, BIG_TIMES)
        self.image = self.born_image
        self.rect = self.image.get_rect()

        # 设置战机初始位置，x轴中间，y轴 85%位置
        self.rect.centerx = SCREEN_RECT.width / 2
        self.rect.y = self.rect.height + SCREEN_RECT.height

        # 设置子弹精灵组
        self.bullets = pygame.sprite.Group()

    def born(self):
        # 飞机无敌
        # 飞机高亮显示
        self.image = self.born_image
        self.isUnbeatable = self.rect.y >= (0.9 * SCREEN_RECT.height)
        # 飞机从屏幕下方飞出
        if self.isUnbeatable:
            self.rect.y = self.rect.y - 2

    def update(self):
        super(Player, self).update()
        self.rect.x = self.rect.x + self.x_speed
        self.rect.y = self.rect.y + self.y_speed

        # 判断英雄飞机是否越界
        if self.rect.x <= 0:
            self.rect.x = 0
        if self.rect.y <= 0:
            self.rect.y = 0
        if self.rect.right >= SCREEN_RECT.width:
            self.rect.right = SCREEN_RECT.width

    def move_left(self):
        if self.rect.bottom >= SCREEN_RECT.height:
            self.rect.bottom = SCREEN_RECT.height
        self.image = self.left_image
        self.x_speed = -PLAYER_SPEED

    def move_right(self):
        if self.rect.bottom >= SCREEN_RECT.height:
            self.rect.bottom = SCREEN_RECT.height
        self.image = self.right_image
        self.x_speed = PLAYER_SPEED

    def move_up(self):
        if self.rect.bottom >= SCREEN_RECT.height:
            self.rect.bottom = SCREEN_RECT.height
        self.image = self.up_image
        self.y_speed = -PLAYER_SPEED

    def move_down(self):
        if self.rect.bottom >= SCREEN_RECT.height:
            self.rect.bottom = SCREEN_RECT.height
        self.image = self.static_image
        self.y_speed = PLAYER_SPEED

    def not_move(self):
        if self.rect.bottom >= SCREEN_RECT.height:
            self.rect.bottom = SCREEN_RECT.height
        self.image = self.static_image
        self.x_speed = 0
        self.y_speed = 0

    def fire(self):
        # 开火动画

        # 子弹飞出
        bullet = Bullet("Fly_PlayerBullet11")
        # 设置子弹初始位置
        bullet.rect.bottom = self.rect.y
        bullet.rect.centerx = self.rect.centerx
        self.bullets.add(bullet)


class Bullet(pygame.sprite.Sprite):

    def __init__(self, bullet_code):
        super(Bullet, self).__init__()
        bullet_list = load_graphics("../resources/images/Fly/Bullet")
        bullet = get_image(bullet_list[bullet_code], 0, 0, 7, 22, COLOR_KEY, BIG_TIMES)
        self.image = bullet
        self.rect = self.image.get_rect()

    def update(self):
        super().__init__()
        self.rect.y = self.rect.y + BULLET_SPEED

        # 判断子弹是否越界
        if self.rect.x <= 0 or self.rect.y <= 0 or self.rect.right >= SCREEN_RECT.width:
            self.kill()
